import pymysql.cursors


class MYSQL:
    def __init__(self, host=None, user=None, password=None, db=None):
        """
        db: 指定数据库。
        """
        self.c = pymysql.connect(host=host, user=user, password=password, db=db, charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor)

    def query(self, sql):
        """
        查询操作。
        :param sql:
        :return: 查询的字典结果。
        """
        datas = []
        with self.c.cursor() as cc:
            rows = cc.execute(sql)
            for line in range(rows):
                data = cc.fetchone()
                datas.append(data)
        return datas

    def update(self, sql):
        """
        更新操作。
        :param sql:
        :return:
        """
        with self.c.cursor() as cc:
            cc.execute(sql)
        self.c.commit()

    def insert(self, sql):
        pass

